"""
 Copyright (c) 2022 Huawei Technologies Co.,Ltd.
"""

"""
Case Type   : Spq插件关闭
Case Name   : Spq插件关闭
Create At   : 2024/04/26
Owner       : oliver
Description :
            1.关闭数据库
            2.还原postgresql.conf和pg_hba.conf
            3.开启数据库
Expect      :
            1.关闭数据库成功
            2.还原配置文件成功
            3.开启数据库成功
"""

import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger

class   Stopspq(unittest.TestCase) :
    def setUp(self):
        self.log=Logger()
        self.log.info("-----------this is stop spq-----------")
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.constant = Constant()
        self.commsh_pri = CommonSH('PrimaryDbUser')

    def test_spq(self):
        self.log.info('-----恢复环境-----')
    def tearDown(self) :
       text = '--step1:关闭数据库  expect:成功--'
       stop_clu = self.commsh_pri.stop_db_cluster()
       self.log.info(stop_clu)

       text = '--step2:还原配置文件 expect:成功--'
       recover1 = self.commsh_pri.exec_gs_ssh("rm -f $PGDATA/postgresql.conf; mv $PGDATA/postgresql_spq.conf $PGDATA/postgresql.conf")
       self.log.info(recover1)
       recover2 = self.commsh_pri.exec_gs_ssh("rm -f $PGDATA/pg_hba.conf; mv  $PGDATA/pg_hba_spq.conf $PGDATA/pg_hba.conf")
       self.log.info(recover2)

       text = '--step3:开启数据库  expect:成功--'
       start_clu = self.commsh_pri.start_db_cluster()
       self.log.info(start_clu)
       self.assertTrue(stop_clu,f'关闭数据库失败：{text}')
       self.assertEqual(recover1.count('[SUCCESS]'), 3, f'执行失败:{text}')
       self.assertEqual(recover2.count('[SUCCESS]'), 3, f'执行失败:{text}')
       self.assertTrue(start_clu,f'开启数据库失败：{text}')
       self.log.info(f"-----{os.path.basename(__file__)} eind-----")
